
#include "Product.h"

#include <assert.h>
#include <QDebug>

using namespace ecocity;

static const QString per_kilo_string = "POR_KILO";
static const QString per_unit_string = "POR_UNIDAD";
static const float rate_coope = 0.20;
static const float rate_shop = 0.25;
static const float rate_end_customer = 0.30;

//---------------------------------------------------------------

Product::Product( int id,
                  const QString& name,
                  const QString& description,
                  const Proveider& provider,
                  float price,
                  Product::PRICE_TYPE price_type,
                  uint tax_rate,
                  uint profit_rate )
    : m_id( id ),
      m_name( name ),
      m_description( description ),
      m_provider( provider ),
      m_tax_rate( tax_rate ),
      m_raw_price( price ),
      m_price_type( price_type ),
      m_profit_rate( profit_rate )
{
    update_costs( );
}

//---------------------------------------------------------------

int
Product::id( ) const
{
    return m_id;
}

//---------------------------------------------------------------

const QString&
Product::name( ) const
{
    return m_name;
}

//---------------------------------------------------------------

const QString&
Product::description( ) const
{
    return m_description;
}

//---------------------------------------------------------------

float
Product::raw_price( ) const
{
    return m_raw_price;
}

//---------------------------------------------------------------

float
Product::untaxed_price( Customer::TYPE customer_type ) const
{
    switch ( customer_type )
    {
    case Customer::FRIENDS:
        return m_raw_price;
    case Customer::COOPERATIVA:
        return m_price_coope;
    case Customer::TIENDA:
        return m_price_shop;
    case Customer::CONSUMIDOR_FINAL:
        return m_price_end_customer;
    default:
        assert( 0 );
        break;
    }

    return 999999;
}

//---------------------------------------------------------------

float
Product::taxes( Customer::TYPE customer_type ) const
{
    return taxed_price( customer_type ) - untaxed_price( customer_type );
}

//---------------------------------------------------------------

float
Product::taxed_price( Customer::TYPE customer_type ) const
{
    return untaxed_price( customer_type ) + ( m_raw_price * m_tax_rate / 100 );
}

//---------------------------------------------------------------

uint
Product::tax_rate( ) const
{
    return m_tax_rate;
}

//---------------------------------------------------------------

uint
Product::profit_rate( ) const
{
    return m_profit_rate;
}

//---------------------------------------------------------------

const Proveider&
Product::proveider( ) const
{
    return m_provider;
}

//---------------------------------------------------------------

Product::PRICE_TYPE
Product::price_type( ) const
{
    return m_price_type;
}

//---------------------------------------------------------------

void
Product::update_costs( )
{
    m_price_coope = m_raw_price + ( rate_coope * m_raw_price );
    m_price_shop = m_raw_price + ( rate_shop * m_raw_price );
    m_price_end_customer = m_raw_price + ( rate_end_customer * m_raw_price );
}

//---------------------------------------------------------------

QString
Product::to_string( PRICE_TYPE price_type )
{
    switch ( price_type )
    {
    case PER_KILO: return per_kilo_string;
    case PER_UNIT: return per_unit_string;
    default:
    {
        assert( 0 );
        return per_unit_string;
    }

    }
}

//---------------------------------------------------------------

Product::PRICE_TYPE
Product::to_price_type( const QString& price_type )
{
    if ( price_type == per_kilo_string )
    {
        return PER_KILO;
    }
    else if ( price_type == per_unit_string )
    {
        return PER_UNIT;
    }
    else
    {
        //assert( 0 );
        return PER_UNIT;
    }
}

//---------------------------------------------------------------

bool
Product::reset( const Product &product )
{
    // Suposes id is the same
    if ( product.id() != m_id )
    {
        assert( 0 );
        return false;
    }

    m_name = product.name( );
    m_description = product.description( );
    m_raw_price = product.raw_price( );
    m_price_type = product.price_type( );
    m_provider = product.proveider( );
    m_tax_rate = product.tax_rate( );
    m_profit_rate = product.profit_rate( );

    update_costs( );

    return true;
}
